package de.fstyle.android.location;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.Context;
import android.graphics.Typeface;
import android.location.Location;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class LayoutHelper {

	public static final String DATE_FORMAT_STRING = 
			"yyyy-MM-dd\nHH:mm:ss z";
	public static final DateFormat DATE_FORMAT = 
			new SimpleDateFormat(DATE_FORMAT_STRING, Locale.US);
	
	public static final String LABEL_LABEL = "label";
	public static final String LABEL_MESSAGE = "message";
	public static final String LABEL_PROVIDER = "provider";
	public static final String LABEL_TIME = "time";
	public static final String LABEL_LONGITUDE = "longitude";
	public static final String LABEL_LATITUDE = "latitude";
	public static final String LABEL_ACCURACY = "accuracy";

	public static final String POSTFIX_LABEL = "_label";
	public static final String POSTFIX_VALUE = "_value";
	public static final String POSTFIX_ROW = "_row";
	
	public static final String NOT_INITIALIZED = "not initialized";
	public static final String LOCATION_IS_NULL = "location is null";
	
	public static void visibilityRows(
			TableLayout tableLayout, 
			int visibility, 
			boolean includeLabel) {
		
		for (int i = 0; i < tableLayout.getChildCount(); i++) {
			View child = tableLayout.getChildAt(i);
			if (includeLabel || !child.getTag().equals(LABEL_LABEL + POSTFIX_ROW)) {
				tableLayout.getChildAt(i).setVisibility(visibility);
			}
		}
	}
	
	public static void messageRow(TableLayout tableLayout, String message) {
		visibilityRows(tableLayout, View.GONE, false);
		
		View messageRow = 
				tableLayout.findViewWithTag(LABEL_MESSAGE + POSTFIX_ROW);
		TextView messageText = (TextView) 
				tableLayout.findViewWithTag(LABEL_MESSAGE + POSTFIX_LABEL);

		messageText.setText(message);
		messageRow.setVisibility(View.VISIBLE);
	}

	public static void updateRow(
			TableLayout tableLayout,
			Location location, 
			boolean showProvider) {
		
		if (location == null) {
			messageRow(tableLayout, LOCATION_IS_NULL);
			return;
		}
		
		View messageRow = 
				tableLayout.findViewWithTag(LABEL_MESSAGE + POSTFIX_ROW);
		messageRow.setVisibility(View.GONE);
		
		TableRow providerRow = 
				(TableRow) tableLayout.findViewWithTag(LABEL_PROVIDER + POSTFIX_ROW);
		TextView providerText = 
				(TextView) tableLayout.findViewWithTag(LABEL_PROVIDER + POSTFIX_VALUE);
		
		TableRow timeRow = 
				(TableRow) tableLayout.findViewWithTag(LABEL_TIME + POSTFIX_ROW);
		TextView timeText = 
				(TextView) tableLayout.findViewWithTag(LABEL_TIME + POSTFIX_VALUE);
		
		TableRow longitudeRow = 
				(TableRow) tableLayout.findViewWithTag(LABEL_LONGITUDE + POSTFIX_ROW);
		TextView longitudeText = 
				(TextView) tableLayout.findViewWithTag(LABEL_LONGITUDE + POSTFIX_VALUE);
		
		TableRow latitudeRow = 
				(TableRow) tableLayout.findViewWithTag(LABEL_LATITUDE + POSTFIX_ROW);
		TextView latitudeText = 
				(TextView) tableLayout.findViewWithTag(LABEL_LATITUDE + POSTFIX_VALUE);
		
		TableRow accuracyRow = 
				(TableRow) tableLayout.findViewWithTag(LABEL_ACCURACY + POSTFIX_ROW);
		TextView accuracyText = 
				(TextView) tableLayout.findViewWithTag(LABEL_ACCURACY + POSTFIX_VALUE);
		
		if (showProvider) {
			providerRow.setVisibility(View.VISIBLE);
			providerText.setText(location.getProvider());
		}

		timeRow.setVisibility(View.VISIBLE);
		timeText.setText(DATE_FORMAT.format(new Date(location.getTime())));
		
		longitudeRow.setVisibility(View.VISIBLE);
		longitudeText.setText(location.getLongitude() + "");
		
		latitudeRow.setVisibility(View.VISIBLE);
		latitudeText.setText(location.getLatitude() + "");
		
		accuracyRow.setVisibility(View.VISIBLE);
		accuracyText.setText(location.getAccuracy() + "");
	}
	
	public static TableLayout createTable(String label, Context context) {
		
		// create table layout
		
		TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
				TableLayout.LayoutParams.WRAP_CONTENT, 
				TableLayout.LayoutParams.WRAP_CONTENT);
		tableParams.bottomMargin = 20;
		
		TableLayout tableLayout = new TableLayout(context);
		tableLayout.setLayoutParams(tableParams);
		
		// add label
		
		TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(
				TableLayout.LayoutParams.WRAP_CONTENT, 
				TableLayout.LayoutParams.WRAP_CONTENT);
		
		TableRow labelRow = new TableRow(context);
		labelRow.setTag(LABEL_LABEL + POSTFIX_ROW);
		labelRow.setLayoutParams(rowParams);
		tableLayout.addView(labelRow);

		TableRow.LayoutParams labelParams = new TableRow.LayoutParams(
				TableRow.LayoutParams.WRAP_CONTENT, 
				TableRow.LayoutParams.WRAP_CONTENT);
//		labelParams.span = 2;
		
		TextView labelText = new TextView(context);
		labelText.setTag(LABEL_LABEL + POSTFIX_LABEL);
		labelText.setLayoutParams(labelParams);
		labelText.setTypeface(Typeface.DEFAULT_BOLD);
		labelText.setText(label == null ? "" : label.toUpperCase(Locale.US));
		labelRow.addView(labelText);
		
		if (label == null) {
			labelText.setVisibility(View.GONE);
		}

		// add message
		
		TableRow messageRow = new TableRow(context);
		messageRow.setTag(LABEL_MESSAGE + POSTFIX_ROW);
		messageRow.setLayoutParams(tableParams);
		tableLayout.addView(messageRow);

		TextView messageText = new TextView(context);
		messageText.setTag(LABEL_MESSAGE + POSTFIX_LABEL);
		messageText.setLayoutParams(labelParams);
		messageText.setText(NOT_INITIALIZED);
		messageRow.addView(messageText);

		// add location information
		
		TableRow provider = createRow(LABEL_PROVIDER, "", context);
		provider.setVisibility(View.GONE);
		tableLayout.addView(provider);
		
		TableRow longitude = createRow(LABEL_LONGITUDE, "", context);
		longitude.setVisibility(View.GONE);
		tableLayout.addView(longitude);
		
		TableRow latitude = createRow(LABEL_LATITUDE, "", context);
		latitude.setVisibility(View.GONE);
		tableLayout.addView(latitude);
		
		TableRow accuracy = createRow(LABEL_ACCURACY, "", context);
		accuracy.setVisibility(View.GONE);
		tableLayout.addView(accuracy);

		TableRow time = createRow(LABEL_TIME, "", context);
		time.setVisibility(View.GONE);
		tableLayout.addView(time);
		
		return tableLayout;
		
	}
	
	public static TableRow createRow(
			String label,
			String value,
			Context context) {

		TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
				TableRow.LayoutParams.WRAP_CONTENT, 
				TableRow.LayoutParams.WRAP_CONTENT);
		
		TableRow tableRow = new TableRow(context);
		tableRow.setTag(label + POSTFIX_ROW);
		tableRow.setLayoutParams(tableParams);

		TableRow.LayoutParams labelParams = new TableRow.LayoutParams(
				TableRow.LayoutParams.WRAP_CONTENT, 
				TableRow.LayoutParams.WRAP_CONTENT);
		labelParams.rightMargin = 20;
		
		TextView labelView = new TextView(context);
		labelView.setTag(label + POSTFIX_LABEL);
		labelView.setLayoutParams(labelParams);
		labelView.setText(label);
		tableRow.addView(labelView);
		
		TextView valueView = new TextView(context);
		valueView.setTag(label + POSTFIX_VALUE);
		valueView.setLayoutParams(labelParams);
		valueView.setText(value);
		tableRow.addView(valueView);
		
		return tableRow;
		
	}
	
	public static String locationString(Location location) {

		StringBuffer buffer = new StringBuffer();
		
		if (location == null) {
			buffer.append("location was null\n");
		} else {
			buffer.append("provider:  " + location.getProvider() + "\n");
			buffer.append("longitude: " + location.getLongitude() + "\n");
			buffer.append("latitude:  " + location.getLatitude() + "\n");
			buffer.append("accuracy:  " + location.getAccuracy() + "\n");
			buffer.append("timestamp: " + DATE_FORMAT.format(location.getTime()) + "\n");
		}
		
		return buffer.toString();
	}
}
